package com.example.huwang.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.huwang.dialog.WarningDialog;
import com.example.huwang.utils.AppConfig;
import com.example.huwang.utils.SMSContentObserver;

import java.util.Arrays;

public class BaseActivity extends AppCompatActivity {
    private SMSContentObserver smsContentObserver;
    protected static final int MSG_INBOX = 1;
    WarningDialog warningDialog;
    BroadcastReceiver dangerCallReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String phone = intent.getStringExtra("callNum");
            if (!Arrays.asList(AppConfig.dangerPhoneArr).contains(phone)) {
                return;
            }
            if (warningDialog == null) {
                warningDialog = new WarningDialog(BaseActivity.this);
            }
            if (!warningDialog.isShowing()) {
                warningDialog.show(phone);
            }


        }
    };

    private void showSMSdanger(String smsContent) {
        if (AppConfig.smsDangerStr.contains(smsContent)) {
            if (warningDialog == null) {
                warningDialog = new WarningDialog(BaseActivity.this);
            }
            if (!warningDialog.isShowing()) {
                warningDialog.show(smsContent);
            }
        }


    }


    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_INBOX:
                    setSmsCode();
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.huwang.DANGER_CALL");
        registerReceiver(dangerCallReceiver, filter);

        if (smsContentObserver != null) {
            getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsContentObserver);
        }

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        smsContentObserver = new SMSContentObserver(this, mHandler);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (dangerCallReceiver != null) {
            unregisterReceiver(dangerCallReceiver);
        }
        if (smsContentObserver != null) {
            getContentResolver().unregisterContentObserver(smsContentObserver);// 取消监听短信数据库的变化
        }
    }

    private void setSmsCode() {
        Cursor cursor = null;
        // 添加异常捕捉
        try {
            cursor = getContentResolver().query(
                    Uri.parse("content://sms/inbox"),
                    new String[]{"_id", "address", "read", "body", "date"},
                    null, null, "date desc"); // datephone想要的短信号码
            if (cursor != null) { // 当接受到的新短信与想要的短信做相应判断
                String body = "";
                while (cursor.moveToNext()) {
                    body = cursor.getString(cursor.getColumnIndex("body"));// 在这里获取短信信息
                    long smsdate = Long.parseLong(cursor.getString(cursor
                            .getColumnIndex("date")));
                    long nowdate = System.currentTimeMillis();
                    // 如果当前时间和短信时间间隔超过60秒,认为这条短信无效
                    if (nowdate - smsdate > 60 * 1000) {
                        break;
                    }
                    showSMSdanger(body);
                  

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}
